package cc.messcat.util;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class LevelUtil {
	
	public static final Integer ALL = 0;
	public static final Integer PROVINCE = 1;
	public static final Integer CITY = 2;
	public static final Integer COUNTY = 3;
	public static final Integer TOWN = 4;
	public static final Integer VILLAGE = 5;
	
    public enum Level {
        ALL(0, "", ""),
        PROVINCE(1, "province", "provinceId"),
        CITY(2, "city", "cityId"),
        CTY(3, "cty", "countyId"),
        CRY(4, "cry", "countryId"),
        COUNTRY(5, "name", "villageId");
        Integer nubmer;
        String name;
        String idName;

        Level(int nubmer, String name, String idName) {
            this.nubmer = nubmer;
            this.name = name;
            this.idName = idName;
        }

        public static String getLevelName(Integer num) {
            switch (num) {
                case 0:
                    return ALL.name;
                case 1:
                    return PROVINCE.name;
                case 2:
                    return CITY.name;
                case 3:
                    return CTY.name;
                case 4:
                    return CRY.name;
                case 5:
                    return COUNTRY.name;
                default:
                    return null;
            }
        }

        public static String getLevelIdName(Integer num) {
            switch (num) {
                case 0:
                    return ALL.idName;
                case 1:
                    return PROVINCE.idName;
                case 2:
                    return CITY.idName;
                case 3:
                    return CTY.idName;
                case 4:
                    return CRY.idName;
                case 5:
                    return COUNTRY.idName;
                default:
                    return null;
            }
        }
    }

    /**
     * 将一个 Map 对象转化为一个 JavaBean
     *
     * @param type 要转化的类型
     * @param map  包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException    如果分析类属性失败
     * @throws IllegalAccessException    如果实例化 JavaBean 失败
     * @throws InstantiationException    如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    @SuppressWarnings("rawtypes")
    public static Object convertMap(Object obj, Map map)
            throws IntrospectionException, IllegalAccessException,
            InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); // 获取类属性
        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();

            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                Object value = map.get(propertyName);

                Object[] args = new Object[1];
                args[0] = value;

                descriptor.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }

    /**
     * 判断是否为Ajax请求
     * <功能详细描述>
     *
     * @param request
     * @return 是true, 否false
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String header = request.getHeader("X-Requested-With");
        if (header != null && "XMLHttpRequest".equals(header))
            return true;
        else
            return false;
    }
}
